package cc.vv.btpartyjob.partymanager.view

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.vFinal.BTKey
import cc.vv.baselibrary.view.util.DateChoseDialog
import cc.vv.btpartyjob.jnimodule.server.OperationServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.inter.YQApplyTimeClickInterface
import cc.vv.btpartyjob.partymanager.moudel.bean.ProjectDetailJieDuanObj
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lklibrary.log.LogOperate
import java.text.SimpleDateFormat
import java.util.*

/**
 * 项目详情编辑--延期编辑
 * Created by  Chen.qi on 2018/10/24.
 */
class ProjectDetailEditView : LinearLayout {
    /**
     * 上下文
     */
    private var mContext: Context? = null
    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null


    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_operation_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_lov_showAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    @SuppressLint("SetTextI18n")
    fun addAllSelectedFileView(optionList: ArrayList<ProjectDetailJieDuanObj>?, applyInterface: YQApplyTimeClickInterface) {
        if (null == optionList) {
            return
        }
        //清除
        clearAllJobOptionView()
        val viewWight = (LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(24f))
        val layoutParams1 = LayoutParams(viewWight, LayoutParams.WRAP_CONTENT)
        for (i in 0 until optionList.size) {
            val view = LayoutInflater.from(mContext).inflate(R.layout.layout_project_course_yq, null)
            view.layoutParams = layoutParams1
            view.setPadding(LKScreenUtil.dp2px(12f), 0, 0, 0)

            //阶段
            val tvJieduanTitle = view.findViewById<TextView>(R.id.tv_lpcYq_courseIntroNum)

            //申请延期
            val tvYanQiStatus = view.findViewById<TextView>(R.id.tv_lpcYq_courseStatus)

            //内容
            val tvContent = view.findViewById<TextView>(R.id.tv_lpcYq_projectContent)

            //预警时间
            val tvYuJingTime = view.findViewById<TextView>(R.id.tv_lpcYq_projectWarningTime)

            //预计完成时间
            val tvCompleteTime = view.findViewById<TextView>(R.id.tv_lpcYq_planCompleteDate)

            //项目状态
            val tvProjectState = view.findViewById<TextView>(R.id.tv_lpcYq_projectStatus)

            //预警
            val lladdYj = view.findViewById<LinearLayout>(R.id.ll_lpcYq_addStartTime_other)
            val tvaddYjTime = view.findViewById<TextView>(R.id.tv_lpcYq_yuJinTime_other)

            //完成时间
            val lladdComplete = view.findViewById<LinearLayout>(R.id.ll_lpcYq_addStartTime_Two)
            val tvaddCompleteTime = view.findViewById<TextView>(R.id.tv_lpcYq_yuJinTime_other_two)


            lladdYj.setOnClickListener({
                chooseStartData(mContext!!, i, applyInterface, tvaddYjTime, 0)
            })

            lladdComplete.setOnClickListener({
                chooseStartData(mContext!!, i, applyInterface, tvaddCompleteTime, 1)
            })


            tvJieduanTitle?.text = "第${i + 1}阶段"
            val addProjectObj = optionList[i]

            val addYJTime = addProjectObj.addYJTime
            if (addYJTime > 1) {
                tvaddYjTime?.text = LKTimeUtil.getInstance().formatTime("yyyy-MM-dd", addProjectObj.addYJTime)
            }
            val addWanChengTime = addProjectObj.addWanChengTime
            if (addWanChengTime > 1) {
                tvaddYjTime?.text = LKTimeUtil.getInstance().formatTime("yyyy-MM-dd", addProjectObj.addWanChengTime)
            }
            tvContent?.text = addProjectObj.content
            tvYuJingTime?.text = OperationServer.formatStr(addProjectObj.warningTime)
            tvCompleteTime?.text = OperationServer.formatStr(addProjectObj.completeTime)
            if (null != addProjectObj.state) {
                when (addProjectObj.state) {
                    0 -> {
                        tvProjectState?.text = "未开始"
                    }
                    1 -> {
                        tvProjectState?.text = "进行中"
                    }
                    2 -> {
                        tvProjectState?.text = "已完成"
                    }
                }
            }

            val tv = TextView(mContext)
            val layoutParamstv = LayoutParams(LKScreenUtil.dp2px(3f), LKScreenUtil.dp2px(8f))
            tv.layoutParams = layoutParamstv
            llSelectedAll?.addView(tv)
            llSelectedAll?.addView(view)
        }
    }

    /**
     * 清除view
     */
    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }


    /**
     * 选择预警时间
     */
    private fun chooseStartData(context: Context, pos: Int,
                                yanQi: YQApplyTimeClickInterface,
                                tvTime: TextView, type: Int) {
        var birthday = ""
        val birthArr = birthday.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var year = Calendar.getInstance().get(Calendar.YEAR)
        var month = Calendar.getInstance().get(Calendar.MONTH)
        var day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
        if (birthArr.size == BTKey.BTKEY_INT_3) {
            try {
                year = Integer.parseInt(birthArr[0])
                month = Integer.parseInt(birthArr[1]) - 1
                day = Integer.parseInt(birthArr[2])
            } catch (e: Exception) {
                LogOperate.e(e.message)
            }
        }
        //初始化显示控件
        DateChoseDialog.getInstance().initBirthDatePicker(context, year, month, day, 3)
        DateChoseDialog.getInstance().setBrithDatenterface { date ->
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val format = sdf.format(date)
            if (type == 0) {
                yanQi?.changeYJtime(pos, format)
            } else if (type == 1) {
                yanQi?.changeCompleteTime(pos, format)
            }
            tvTime?.text = format
        }
        DateChoseDialog.getInstance().showBirthDateDialog()
    }

}